package cn.com.gridinfo.sooc.core.login.common;

import com.jeremy.arad.Arad;
import com.jeremy.arad.utils.Log;

import cn.com.gridinfo.sooc.common.LocalMark;
import cn.com.gridinfo.sooc.core.login.bean.NodeServerInfo;
import cn.com.gridinfo.sooc.core.login.bean.UserInfo;

/**
 * Created by jieli on 16/2/16.
 */
public class UserPreference {

    /**
     * 存储服务器到本地
     */
    public static void setServerPreference(NodeServerInfo nodeServerInfo) {
        Arad.preferences.putString(LocalMark.SERVER_ID.getName(), nodeServerInfo.getSnsid());//节点 snid
        Arad.preferences.putString(LocalMark.SERVER_NAME.getName(), nodeServerInfo.getSqmc());//节点名称
        Arad.preferences.putString(LocalMark.SERVER_URL.getName(), nodeServerInfo.getXtdz());//节点域名
        Arad.preferences.flush();
    }

    /**
     * 存储用户信息到本地到本地
     */
    public static void setUserPreference(UserInfo user) {
        Arad.preferences.putString(LocalMark.USER_UID.getName(), user.getUid());//用户 uid
        Arad.preferences.putString(LocalMark.USER_ACCOUNT.getName(), user.getAccount());//账号
        Arad.preferences.putString(LocalMark.USER_PWD.getName(), user.getPassword());//密码 未加密存储
        Arad.preferences.putString(LocalMark.USER_NICKNAME.getName(), user.getName());//昵称
        Arad.preferences.putString(LocalMark.USER_HEADIMG.getName(), user.getImg());//头像
        Arad.preferences.putString(LocalMark.USER_IDENTITYTYPE.getName(), user.getIdentitytype());
        Arad.preferences.putString(LocalMark.USER_EMAIL.getName(), user.getEmail());
        Arad.preferences.putBoolean(LocalMark.IS_AUTO_LOGIN.getName(), true);//设置自动登录标示符,标示是否登录成功过
        Arad.preferences.flush();
    }

    /**
     * 清除用户数据
     */
    public static void clearUserPrefrence() {
        Arad.preferences.remove(LocalMark.USER_UID.getName());//用户 uid
        Arad.preferences.remove(LocalMark.USER_ACCOUNT.getName());//账号
        Arad.preferences.remove(LocalMark.USER_PWD.getName());//密码 未加密存储
        Arad.preferences.remove(LocalMark.USER_NICKNAME.getName());//昵称
        Arad.preferences.remove(LocalMark.USER_HEADIMG.getName());//头像
        Arad.preferences.remove(LocalMark.USER_IDENTITYTYPE.getName());
        Arad.preferences.remove(LocalMark.USER_EMAIL.getName());
        Arad.preferences.remove(LocalMark.USER_SFZJH.getName());
        Arad.preferences.remove(LocalMark.USER_REALNAME.getName());
        Arad.preferences.remove(LocalMark.USER_TEL.getName());
        Arad.preferences.flush();
    }

    /**
     * 存储用户权限到本地到本地
     */
    public static void setPermisionPreference(UserInfo user) {
        setPermision(LocalMark.IS_DISPLAY_ADS.getName(), user.getAndroid_audit());//是否显示广告
        setPermision(LocalMark.IS_VISITOR.getName(), user.getAndroid_visitor());//是否游客身份
        setPermision(LocalMark.IS_DISPLAY_WALL.getName(), user.getAndroid_show());//是否显示积分墙
        setPermision(LocalMark.IS_PAY.getName(), user.getAndroid_charge());//节点是否收费
    }

    public static void setPermision(String typeName, int isFlag) {
        switch (isFlag) {
            case 1:
                Arad.preferences.putBoolean(typeName, true);
                Log.d(" typename true  : " + typeName);
                break;
            case 0:
                Arad.preferences.putBoolean(typeName, false);
                Log.d(" typename false  : " + typeName);
                break;
        }

        Arad.preferences.flush();
        Log.d("flush  : " + typeName);
    }


    /**
     * 清除本地消息数据
     */
    public static void clearMsgPrefrence() {
        //切换账号，清除我的消息保存在本地的时间标志
        Arad.preferences.remove(LocalMark.MSG_KCINPUT_TIME.getName());
        Arad.preferences.remove(LocalMark.MSG_TLINPUT_TIME.getName());
        Arad.preferences.flush();
    }
}
